package com.cskaoyan.controller.wxgoods;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MemoryBean;
import com.cskaoyan.aspect.AnnoFootprint;
import com.cskaoyan.bean.vo.wxgoods.GoodsCategoryVo;
import com.cskaoyan.bean.vo.wxgoods.GoodsDetailVo;
import com.cskaoyan.bean.vo.wxgoods.GoodsListVo;
import com.cskaoyan.bean.vo.wxgoods.GoodsRelatedVo;
import com.cskaoyan.service.wxgoods.WxGoodsService;
import com.cskaoyan.util.common.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Ajun de BBC
 * @Date 2022/7/19 16:40
 * @Version 1.5
 */

@RequestMapping("wx/goods")
@RestController
public class WxGoodsController {

    @Autowired
    MemoryBean memoryBean;
    @Autowired
    WxGoodsService wxGoodsService;


    //绑定
    private Integer getUserId(@RequestHeader(Constant.WX_TOKEN_HEADER) String token) {
        Integer userId = memoryBean.tokenAndUserId.get(token);
        if (userId == null) {
            return -1;
        }
        return userId;
    }


    @RequestMapping("count")
    public BaseRespVo wxGoodsCount() {
        Integer i = wxGoodsService.goodsCount();
        return BaseRespVo.ok(i, "成功", 0);
    }

    @RequestMapping("category")
    public BaseRespVo wxGoodsCategory(Integer id) {
        GoodsCategoryVo goodsCategoryVo = wxGoodsService.goodsCategory(id);
        return BaseRespVo.ok(goodsCategoryVo, "成功", 0);
    }

    @RequestMapping("list")
    public BaseRespVo wxGoodsList(@RequestHeader(Constant.WX_TOKEN_HEADER) String token,
                                  Integer categoryId, Integer brandId, String keyword, Boolean isNew, Boolean isHot,
                                  Integer page, Integer limit, String sort, String order) {
        Integer userId = getUserId(token);
        if (userId == null) {
            return BaseRespVo.ok(null, "请重新登录", 0);
        }
        GoodsListVo goodsListVo = wxGoodsService.goodsList(userId, categoryId, brandId, keyword, isNew, isHot, page, limit, sort, order);
        return BaseRespVo.ok(goodsListVo, "成功", 0);
    }

    @AnnoFootprint
    @RequestMapping("detail")
    public BaseRespVo wxGoodsDetail(@RequestHeader(Constant.WX_TOKEN_HEADER) String token, Integer id) {
        Integer userId = getUserId(token);
        if (userId == null) {
            return BaseRespVo.ok(null, "请重新登录", 0);
        }
        //zhong 7/21 23:32,查看个人信息需要userId
        GoodsDetailVo goodsDetailVo = wxGoodsService.goodsDetailAll(userId, id);
        return BaseRespVo.ok(goodsDetailVo, "成功", 0);
    }

    @RequestMapping("related")
    public BaseRespVo wxGoodsRelated(@RequestHeader(Constant.WX_TOKEN_HEADER) String token, Integer id) {
        Integer userId = getUserId(token);
        if (userId == null) {
            return BaseRespVo.ok(null, "请重新登录", 0);
        }
        GoodsRelatedVo goodsRelatedVo = wxGoodsService.wxGoodsRelated(userId, id);
        return BaseRespVo.ok(goodsRelatedVo, "成功", 0);
    }

}
